package com.wuxiaofan.service;

import com.alibaba.fastjson.JSONObject;
import com.wuxiaofan.entity.OrderInfo;
import com.wuxiaofan.feign.ProductFeignClient;
import com.wuxiaofan.feign.UserFeignClient;
import com.wuxiaofan.mapper.OrderInfoMapper;
import com.wuxiaofan.util.DateUtils;
import com.wuxiaofan.util.KeyWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class OrderInfoService {


    @Autowired
    private OrderInfoMapper orderInfoMapper;
    @Autowired
    private UserFeignClient userFeignClient;
    @Autowired
    private ProductFeignClient productFeignClient;


    public void updateByMap(Map paraMap){
        orderInfoMapper.updateMapById(paraMap);
    }

    public OrderInfo insert(OrderInfo orderInfo) throws Exception{
        orderInfo.setId(KeyWorker.nextIdStr());
        orderInfo.setCreateTime(DateUtils.getNowDate());
        orderInfoMapper.insert(orderInfo);
        if(true){
            throw new Exception("下单异常");
        }
        return orderInfo;
    }

    public OrderInfo save(JSONObject jsonObject) throws Exception{
        OrderInfo orderInfo = JSONObject.parseObject(jsonObject.toJSONString(),OrderInfo.class);




        jsonObject.put("id",jsonObject.getString("userId"));
        jsonObject.put("gold",jsonObject.getString("gold"));
        userFeignClient.subBalance(jsonObject);


        jsonObject.put("id",jsonObject.getString("productId"));
        jsonObject.put("num",jsonObject.getIntValue("num"));
        productFeignClient.subRepertory(jsonObject);


        insert(orderInfo);
        return orderInfo;
    }
}
